/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Formularios;

import BD.clientes;
import BD.pedidos;
import BD.utilsPim;
import java.awt.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author CORE
 */
  
/*public FormFilho(Form f)  
{  
   InitializeComponent();  
   this.f = f;  
}*/
public class FormComprovante extends javax.swing.JDialog {
    
    public int ped;
    private utilsPim utl = new utilsPim();
    
    private FormCaixa f;  
    private pedidos pd = new pedidos(); 
    /**
     * Creates new form FormComprovante
     */
    public FormComprovante(java.awt.Frame parent, boolean modal, FormCaixa h) {
        super(parent, modal);
        initComponents();
        this.f = h;
        
        //this.ped  = _Ped;
        GeraComprovante(this.ped);
    }
    private void addd (List l,String s,int q){
        for (int i = 0; i < q; i++) {
            l.add(s);            
        }
    }    
    private void GeraComprovante(int  ped){
        
        //pd = this.f.relped;
        ArrayList<pedidos> lst = new ArrayList<pedidos>();
        pd.setId_pedidos(this.f.relnpedido);
        pd.setSiuacao("");
        pd.pesqfinalizado = false;
        pd.setCliente_nome("");
        pd.setMesa(0);
        System.out.println(this.f.relnpedido);        
        lst = pd.getItens();
        pd = lst.get(0);
        System.out.println(pd.getId_pedidos());
        //pd.setId_pedidos(5);
//        pd = pd.getItens().get(0);
//        Pedo = pd;
        addd(l1,"",6);
        l.add("              PIZZARIA MASTER");
        l.add(" AV DAS LOGOAS SN QD 41 LT 10");
        l.add(" BAIRRO DO HILSA-GOIANIA-GOIAS");        
        l.add("");
        l.add("CNPJ :19.161.009/00001-32");
        l.add("IE:10.6568.758-9");
        l.add(pd.getData().toString());
        l.add("      CUPO FISCAL   ");
        l.add("ITEM COD DESCRICAO ");
        
        l1.add("COD:000"+pd.getId_pedidos());        
        addd(l1,"",1);        
        l1.add("VALOR");              
        
        float tot = 0;
        System.out.println(lst.get(0));
        System.out.println(lst.get(0).getListitens().size());
        for (int i = 0; i < lst.get(0).getListitens().size(); i++) {          
            int cod =0;
            String desc="";
            if(lst.get(0).getListitens().get(i).getId_pizzas()>0 ){
                cod = lst.get(0).getListitens().get(i).getId_pizzas();
                desc= lst.get(0).getListitens().get(i).getDesc_pizzas();                
            }else{
                if(lst.get(0).getListitens().get(i).getProdutos() != null )
                {    
                cod = lst.get(0).getListitens().get(i).getProdutos().getId_produtos();
                desc= lst.get(0).getListitens().get(i).getProdutos().getDescricao();                                
            }   }
            l.add((i+1)+"       "+cod+"     "+desc+"    ");                      
            l1.add(utl.formatavalor(pd.getListitens().get(i).getValor()));
            tot = tot +pd.getListitens().get(i).getValor(); 
        }
        l.add("--------------------------------");
        l1.add("------------");
        l.add("Sub Total");
        l1.add(utl.formatavalor(tot));
        l.add("Desconto");
        
        if(! this.f.reldesconto.equals(""))
            l1.add(utl.formatavalor(Float.parseFloat(this.f.reldesconto.replace(",","."))));  
        else
            l1.add("0,00");
        

        l.add("--------------------------------");
        l1.add("------------");
        l.add("Total");
        
        if(! this.f.reldesconto.equals(""))
            l1.add(utl.formatavalor(tot - Float.parseFloat(this.f.reldesconto.replace(",","."))));      
        else
            l1.add(utl.formatavalor(tot));      
        
        dadosCliente();

    }
    private void dadosCliente(){
        clientes cli =  new clientes();
        if(this.pd.getId_clientes()>0)
            cli.setId_cliente(this.pd.getId_clientes());

        //caso o pedido seja de mesa não tera cliente logo tem quye ser ficticio
        if(cli.getItens().size()<=0){
            cli.setNome("CONSUMIDOR ESTADUAL");
            cli.setEndereco("GOIAS");
            cli.setFone("(62)9999-9999");
            cli.setCelular("(62)9999-9999");
        }else
            cli = cli.getItens().get(0);            
        
        addd(l, null, 5);
        l.add("CNPJ/CPF.:111.111.111/11");
        l.add("Cliente..:"+cli.getNome());
        l.add("Endereco.:"+cli.getEndereco());
        l.add("Celular.:"+cli.getCelular());
        l.add("Telefone.:"+cli.getFone());
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        l = new java.awt.List();
        l1 = new java.awt.List();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("COMPROVATE DE COMPRA");

        l.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        l1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        l1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                l1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(l, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
            .addComponent(l1, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void l1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_l1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_l1ActionPerformed

    /**
     * @param args the command line arguments
     */
    /*
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
/*        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormComprovante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormComprovante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormComprovante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormComprovante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FormComprovante dialog = new FormComprovante(new javax.swing.JFrame(), true,0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.List l;
    private java.awt.List l1;
    // End of variables declaration//GEN-END:variables
}
